# coding: utf-8

import sys
sys.path.append('../3_data_structure')
from stack import Stack


def postfix_evaluation(expression):
    stack = Stack()
    items = expression.split(' ')

    for item in items:
        try:
            item = int(item)
            stack.push(item)
        except ValueError:
            operand2 = stack.pop()
            operand1 = stack.pop()
            stack.push(eval(str(operand1) + item + str(operand2)))

    return stack.pop()


# 等效：( 2 + 3 ) * ( 5 - 3 )
print(postfix_evaluation('2 3 + 5 3 - *'))

# 等效：9 * (8 + 1)
print(postfix_evaluation('9 8 1 + *'))

print(postfix_evaluation('17 10 + 3 * 9 /'))

